package fr.pud.client.controller.fleet;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Fleet;
import fr.pud.client.model.Vehicle;
import fr.pud.client.view.MainFrame;
import fr.pud.utils.Tools;

/**
 * Enables to save a fleet into a text file. Method use : Serialization.
 * Apr 7, 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         SaveFleet.java
 */
public class SaveFleet extends AbstractAction {
    /**
     * The serial UID
     */
    private static final long serialVersionUID = -757889977409813899L;

    /**
     * Default constructor
     */
    public SaveFleet() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.SAVE_FLEET));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.SAVE_FLEET));
        this.putValue(SMALL_ICON, ImagesDatabase.SAVE);
    }

    /**
     * Serializes the fleet into a file.
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        String fileName = JOptionPane.showInputDialog(MainFrame.getSingleton(),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.INPUT_FILE_NAME),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.SAVE_FLEET),
                JOptionPane.PLAIN_MESSAGE);
        ArrayList<Vehicle> vehicles = Fleet.getInstance().getVehicles();
        Tools.saveObject(vehicles, fileName, "fleet");
    }
}
